<?php

namespace app\backend\controller\sys;

use app\common\controller\Basic;
use app\common\service\sys\RoleService;
use app\common\service\sys\UserRoleMenusService;

/**
 * 角色
 * @author kaige
 * @update 2022-0428
 */
class Role extends Basic
{
  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new RoleService();
  }
  /**
   * 角色列表
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      return json($this->service->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   */
  public function add()
  {
    if ($this->request->isPost()) {
      if (!input('?param.name')) {
        return _jerr(1002);
      }
      $data = input('param.');
      $this->_form_filter($data);
      if ($this->service->add($data)) return _jok(1023);
      return _jerr(1024);
    }
    if ($this->request->isGet()) {
      return _jok('ok');
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   */
  public function edit()
  {
    $id = input('id/d', 0);
    if ($this->request->isPost()) {
      if (!input('?param.name')) {
        return _jerr(1002);
      }
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $data = $this->service->getInfoById($id);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   * @param array $data
   */
  protected function _form_filter(&$data)
  {
    url_decode($data);
    if (isset($data['user_ids']) && !empty($data['user_ids'])) {
      $data['user_ids'] = parse_ugdc($data['user_ids']);
      $data['user_ids'] = implode(',', $data['user_ids']);
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id/d', 0);
      if ($id === 0) {
        return _jerr(1002);
      }
      if ($this->service->delete($id)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }
    return _jerr(1000);
  }

  /**
   * 权限配置 -- 结果与前端对应的json
   */
  public function setauz()
  {
    $return_data = ['code' => 0, 'msg' => '获取成功', 'data' => []];
    if (!input('?param.id')) {
      return _jerr(1002);
    }
    //配置当前管理人员所拥有的
    $re = $this->service->getRoleMenuData(input('id/d', 0), $this->user['id']);
    if (!$re['status']) {
      return json($re);
    }
    $return_data['data'] = $re['data'];
    return json($return_data);
  }

  /**
   * 保存权限配置
   */
  public function savePerm()
  {
    if ($this->request->isPost()) {
      $post = input('param.');
      if (!isset($post['id'])) {
        return _jerr(1002);
      }
      if (!isset($post['data'])) {
        return _jerr(1002);
      }
      //保存角色信息
      $manageRoleInfo = $this->service->getInfoById($post['id']);
      if (!$manageRoleInfo) {
        return _jerr('查无此角色');
      }
      $urmService = new UserRoleMenusService();
      $result = $urmService->savePerm($post['id'], $post['data']);
      return _jok('设置成功', $result);
    }
    return _jerr(1000);
  }

  /**
   * 获取角色用户列表
   */
  public function getroleuser()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      if (isset($data['id']) &&  $data['id'] > 0) {
        $out = $this->service->getroleuser($data);
        return json($out);
      }
    }
    return _jerr(1000);
  }
}
